<?php

namespace App\Models\Flight;

use App\Models\Rbac\GuardUser;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\Common\CommonModel;
class Order extends CommonModel
{
    use HasFactory;

    protected $table = 'order';

    public function getFlightInfo() {
        return $this->hasOne(FlightInfo::class,'id','flight_id');
    }
    public function getCoupleFlightInfo() {
        return $this->hasOne(FlightInfo::class,'id','couple_flight_id');
    }

    public function getWriter() {
        return $this->hasOne(GuardUser::class,'id','writer_id');
    }

    public function getLst(array $conditions = []) {
        $records = parent::getLst($conditions); // TODO: Change the autogenerated stub
        if(!empty($conditions['flight_no'])){
            $records = $records->where('flight_no',$conditions['flight_no']);
        }
        if(!empty($conditions['code'])){
            $records = $records->where('code',$conditions['code']);
        }
        if(isset($conditions['type']) && $conditions['type']!=2){
            $records = $records->where('is_double',$conditions['type']);
        }
        if(!empty($conditions['writerName'])){
            $records = $records->whereHas('getWriter',function($q) use ($conditions){
                $q->where('name','like','%'.$conditions['writerName'].'%');
            });
        }
        if(!empty($conditions['startDate'])){
            $records = $records->whereHas('getFlightInfo',function($q) use ($conditions){
                $q->where('date',$conditions['startDate']);
            });
        }


        return $records->where('is_hide',0);
    }
}
